//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    userInfo: {},
    selectedIndex:0,
    herolist:["","","","",""],
    images:{},
    keyword:"",
    localQuery:[],   /// 存储的查询条件
    all:{}
  },
  //事件处理函数
  bindViewTap: function(e) {
    this.setData({keyword:e.currentTarget.dataset.s});
    this.startSearch({detail:{value:e.currentTarget.dataset.s}});
  },
  /// tab点击
  tabBarTaped: function(e) {
    this.setData({selectedIndex:e.currentTarget.dataset.index});
    this._loadData();
  },
  /// 下载试神数据
  _loadData: function(keyword)
  {
    var query="0";
    if(keyword)
    {
      query = encodeURI("0&name="+keyword);
    }
    else
    {
      if(typeof this.data.herolist[this.data.selectedIndex] == "object")
      {
        return;
      }
      if(this.data.selectedIndex==0)
        query = "0";
      else
        query = (5-this.data.selectedIndex)+"";
    }
    var self = this;
    wx.showLoading({"title":"获取数据中"});
    wx.request({
      url:'https://g37simulator.webapp.163.com/get_heroid_list?callback=cb&rarity='+(query)+'&page=1&per_page=200&_='+(new Date()).getTime(),
      dataType:'text',
      success:function(res)
      {
        try {
          var data = JSON.parse(res.data.substring(3,res.data.length-1));
          /*
          /// 这里用于打印所有式神
          var dd = "";
          for(var item in data.data)
          {
            dd += (item+",");
          }
          console.log(dd);
          */
          if(data.success==true)
          {
            if(data.total_num==0)
            {
              wx.showToast({"title":"无该式神!"});
              return; 
            }
            var tmpIndex = self.data.selectedIndex;
            for(var heroId in data.data)
            {
                data.data[heroId].image = "http://moysky.qiniudn.com/yys/avator/"+heroId+".png";
                data.data[heroId].heroId = heroId;
            }
            if(!keyword)
            {
              switch(tmpIndex)
              {
                case 0:
                {
                  self.setData({'herolist[0]':(data.data)});
                  self.data.all = data.data;
                }
                  break;
                case 1:
                  self.setData({'herolist[1]':(data.data)});
                  break;
                case 2:
                  self.setData({'herolist[2]':(data.data)});
                  break;
                case 3:
                  self.setData({'herolist[3]':(data.data)});
                  break;
                case 4:
                  self.setData({'herolist[4]':(data.data)});
                  break;
                default:
                  break;
              }
            }
            else
            {
              self.setData({'herolist[0]':(data.data)});
              for(var i=0;i<self.data.localQuery.length;i++)
              {
                if(keyword==self.data.localQuery[i])
                  return;
              }
              /// 显示搜索关键字
              self.data.localQuery.unshift(keyword);
              /// 只留7个
              if(self.data.localQuery.length>7)
              {
                self.data.localQuery.pop();
              }
              self.setData({localQuery:self.data.localQuery});
              /// 存储一下
              wx.setStorage({key:"heroLocalQuery",data:self.data.localQuery});
            }
          }
          else
          {
            wx.showToast({"title":"获取试神数据失败"});
          }
        } catch (error) {
           wx.showToast({"title":"获取试神数据失败"});
        }
      },
      fail:function()
      {
        wx.showToast({"title":"获取试神数据失败"});
      },
      complete:function(){
        self.loading = false;
        wx.hideLoading()
      }
    });
  },
  /// 搜索
  startSearch: function(e){
    if(this.data.selectedIndex!=0)
    {
      this.setData({selectedIndex:0});
    }
    var keyword = e.detail.value;
    if(keyword.length>0)
    {
      
      this._loadData(keyword);
    }
    else
      this.setData({'herolist[0]':this.data.all});
  },
  /// 用户恢复所有式神
  recoverSearch: function(e)
  {
     var keyword = e.detail.value;
     if(keyword==0)
     {
       this.setData({'herolist[0]':this.data.all});
     }
  },
  /// 查看详细
  showHeroDetail: function(e)
  {
    var heroId = e.currentTarget.dataset.hid;
    var rarity = e.currentTarget.dataset.rarity;
    wx.navigateTo({url:("/pages/index/heroDetail/heroDetail?heroId="+heroId+"&rarity="+rarity)});
  },
  //
  onLoad: function () {
    var that = this
    wx.getStorage({key:"heroLocalQuery",success:function(res){
      that.setData({localQuery:res.data});
    }});
    this._loadData();
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  }
})
